package com.kzj.mls.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;

import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.kzj.mls.R;
import com.kzj.mls.beans.CropImageBean;
import com.kzj.mls.util.BitmapUtils;

import java.io.File;
import java.io.IOException;

import de.greenrobot.event.EventBus;


public class CropImageActivity extends GBaseActivity implements View.OnClickListener{


    private static final int REQUEST_GALLERY = 21;
    private static final String TAG = "MainActivity";


    private Bitmap mBitmap;
    private boolean isSnappedToCenter = false;

    private FrameLayout mTop_frame;
    private com.fenchtose.nocropper.CropperView mImageView;
    private ImageView mSnap_button;
    private ImageView mRotate_button;
    private Button mCrop_button;
    private String imageUrl,activityString;
    public static String ImageUrlTag="imageUrl";
    public static String ActivityTag="activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_crop_image);
        findViewByIds();
        initDatas();
    }

    @Override
    protected void findViewByIds() {
        mTop_frame = (FrameLayout) findViewById(R.id.top_frame);
        mImageView = (com.fenchtose.nocropper.CropperView) findViewById(R.id.imageView_crop);
        mSnap_button = (ImageView) findViewById(R.id.snap_button);mSnap_button.setOnClickListener(this);
        mRotate_button = (ImageView) findViewById(R.id.rotate_button);mRotate_button.setOnClickListener(this);
        mCrop_button = (Button) findViewById(R.id.crop_button);mCrop_button.setOnClickListener(this);

    }

    @Override
    protected void initDatas() {
        imageUrl=getIntent().getStringExtra(ImageUrlTag);
        activityString=getIntent().getStringExtra(ActivityTag);
        loadNewImage(imageUrl);
        setTopTitle("图片选择");
        setLeftImg(View.VISIBLE,R.drawable.selector_back);
    }

    @Override
    protected void fillArrayInListView() {

    }


    private void loadNewImage(String filePath) {
        mBitmap = BitmapFactory.decodeFile(filePath);
        Log.i(TAG, "bitmap: " + mBitmap.getWidth() + " " + mBitmap.getHeight());

        int maxP = Math.max(mBitmap.getWidth(), mBitmap.getHeight());
        float scale1280 = (float)maxP / 1280;

        if (mImageView.getWidth() != 0) {
            mImageView.setMaxZoom(mImageView.getWidth() * 2 / 1280f);
        } else {

            ViewTreeObserver vto = mImageView.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    mImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                    mImageView.setMaxZoom(mImageView.getWidth() * 2 / 1280f);
                    return true;
                }
            });

        }

        mBitmap = Bitmap.createScaledBitmap(mBitmap, (int) (mBitmap.getWidth() / scale1280),
                (int) (mBitmap.getHeight() / scale1280), true);
        mImageView.setImageBitmap(mBitmap);
    }




    private void cropImage() {

        Bitmap bitmap = mImageView.getCroppedBitmap();

        if (bitmap != null) {

            try {
                String url=Environment.getExternalStorageDirectory() + "/crop_"+System.currentTimeMillis()+".jpg";
                BitmapUtils.writeBitmapToFile(bitmap, new File(url), 90);
                CropImageBean cropImageBean=new CropImageBean();
                cropImageBean.setActivityString(activityString);
                cropImageBean.setImageUrl(url);
                EventBus.getDefault().post(cropImageBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        finish();
    }

    private void rotateImage() {
        if (mBitmap == null) {
            Log.e(TAG, "bitmap is not loaded yet");
            return;
        }

        mBitmap = BitmapUtils.rotateBitmap(mBitmap, 90);
        mImageView.setImageBitmap(mBitmap);
    }

    private void snapImage() {
        if (isSnappedToCenter) {
            mImageView.cropToCenter();
        } else {
            mImageView.fitToCenter();
        }

        isSnappedToCenter = !isSnappedToCenter;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {

            case R.id.crop_button:
                cropImage();
                break;
            case R.id.rotate_button:
                rotateImage();
                break;
            case R.id.snap_button:
                snapImage();
                break;
            default:
                break;
        }
    }
}
